//
//  DebugRabbitMQManager.m
//  YK_DebugManager
//
//  Created by edward on 2021/1/14.
//

#import "DebugRabbitMQManager.h"

@interface DebugRabbitMQManager ()

@property (nonatomic, strong, readwrite) NSMutableArray<NSDictionary *> *records;

@end

@implementation DebugRabbitMQManager

NSString * const debug_mq_key = @"mqkey";
NSString * const debug_mq_value = @"result";
NSString * const debug_mq_consumerTag =  @"consumerTag";
NSString * const debug_mq_exchangeName =  @"exchangeName";
NSString * const debug_mq_receiveDate =  @"receiveDate";

static DebugRabbitMQManager *_instance;

+ (id)allocWithZone:(struct _NSZone *)zone
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [super allocWithZone:zone];
    });

    return _instance;
}

+ (instancetype)sharedInstance
{
    if (_instance == nil) {
        _instance = [[DebugRabbitMQManager alloc] init];
    }
    return _instance;
}
- (void)saveMQKey:(NSString *)routingKey mqresult:(NSData *)mqresult consumerTag:(NSString *)consumerTag exchangeName:(NSString *)exchangeName receiveDate:(NSDate *)receiveDate
{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        @try {
            if (!(routingKey.length > 0) || !(mqresult.length > 0) || !(consumerTag.length > 0) || !(exchangeName.length > 0) || !receiveDate) {
    #ifdef DEBUG
                //
                NSLog(@"❎参数错误");
    #else
    #endif
                return;
            }
            NSMutableDictionary *mdic = [NSMutableDictionary dictionary];
            [mdic setValue:routingKey forKey:debug_mq_key];
            [mdic setValue:mqresult forKey:debug_mq_value];
            [mdic setValue:consumerTag forKey:debug_mq_consumerTag];
            [mdic setValue:exchangeName forKey:debug_mq_exchangeName];
            [mdic setValue:receiveDate forKey:debug_mq_receiveDate];
            [self.records addObject:[mdic copy]];
        } @catch (NSException *exception) {
            
    #ifdef DEBUG
            //
            NSLog(@"❎保存请求日志错误:%@",exception.reason);
    #else
    #endif
        } @finally {
            
        }
    });
}
- (void)removeItemAtIndex:(NSInteger)index
{
    if (index < self.records.count && index >= 0) {
        [self.records removeObjectAtIndex:index];
    }
}
- (void)removeAllDatas
{
    [self.records removeAllObjects];
}
#pragma mark -----------getter/setter------------
- (NSMutableArray<NSDictionary *> *)records
{
    if (!_records)
    {
        _records = [NSMutableArray array];
    }
    return _records;
}
@end
